package io.renren.admin;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.SysUserEntity;
import io.renren.entity.TPurviewCustinfoEntity;
import io.renren.entity.TPurviewUsermanageEntity;
import io.renren.service.TPurviewCustinfoService;
import io.renren.service.TPurviewUsermanageService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.QueryManage;
import io.renren.utils.R;
import io.renren.utils.ShiroUtils;
import io.renren.utils.httpclient.BizException;


/**
 * 用户管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-02-06 17:41:04
 */
@RestController
@RequestMapping("tpurviewusermanage")
public class TPurviewUsermanageController {
	@Autowired
	private TPurviewUsermanageService tPurviewUsermanageService;
	@Autowired
	private TPurviewCustinfoService tPurviewCustinfoService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("tpurviewusermanage:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils pageUtil = null;
		SysUserEntity obj = ShiroUtils.getUserEntity();
	   if("1".equals(obj.getUserId().toString())){
		   QueryManage query = new QueryManage(params,null);

			List<TPurviewUsermanageEntity> tPurviewUsermanageList = tPurviewUsermanageService.queryListManage(query);
			int total = tPurviewUsermanageService.queryTotal(query);
			
			 pageUtil = new PageUtils(tPurviewUsermanageList, total, query.getLimit(), query.getPage());
	   } else {
		TPurviewCustinfoEntity  tPurviewCustinfoEntity =tPurviewCustinfoService.getinfoByUserName(obj.getUsername());
		//查询列表数据
		QueryManage query = new QueryManage(params,tPurviewCustinfoEntity.getId());

		List<TPurviewUsermanageEntity> tPurviewUsermanageList = tPurviewUsermanageService.queryList(query);
		int total = tPurviewUsermanageService.queryTotal(query);
		 pageUtil = new PageUtils(tPurviewUsermanageList, total, query.getLimit(), query.getPage());
		}
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("tpurviewusermanage:info")
	public R info(@PathVariable("id") String id){
		TPurviewUsermanageEntity tPurviewUsermanage = tPurviewUsermanageService.queryObject(id);
		
		return R.ok().put("tPurviewUsermanage", tPurviewUsermanage);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("tpurviewusermanage:save")
	public R save(@RequestBody TPurviewUsermanageEntity tPurviewUsermanage){
		SysUserEntity obj = ShiroUtils.getUserEntity();
		if("1".equals(obj.getUserId().toString())){
			throw new BizException("admin用户不可以添加云财务用户");
		}
		tPurviewUsermanageService.save(tPurviewUsermanage);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("tpurviewusermanage:update")
	public R update(@RequestBody TPurviewUsermanageEntity tPurviewUsermanage){
		tPurviewUsermanageService.update(tPurviewUsermanage);
		
		return R.ok();
	}
	@RequestMapping("/enabled")
	@RequiresPermissions("tpurviewusermanage:enabled")
	public R enabled(@RequestBody TPurviewUsermanageEntity tPurviewUsermanage){
	    String status = tPurviewUsermanage.getStatus();
		tPurviewUsermanageService.update(tPurviewUsermanage);
		String message = null;
		if("0".equals(status)){
			message = "启用成功";
		}if ("1".equals(status)){
			message  = "禁用成功";
		}
		return R.ok(message);
	}
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("tpurviewusermanage:delete")
	public R delete(@RequestBody String[] ids){
		tPurviewUsermanageService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
